package com.my.hp_hr.contorller;

import com.my.hp_hr.entity.Employee;
import com.my.hp_hr.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/userLoginServlet")
public class UserLoginServlet extends HttpServlet {
    private UserService userService = new UserService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String employeeNumber = req.getParameter("employeeNumber");
            String password = req.getParameter("password");
            Employee employee = userService.userLogin(employeeNumber, password);
            if(employee.getId()>0){
                HttpSession session = req.getSession();
                session.setAttribute("employee",employee);
                req.getRequestDispatcher("home.jsp").forward(req,resp);
                //req.getRequestDispatcher("leaveServlet").forward(req,resp);
            }else{
                req.setAttribute("msg","登陆失败");
                req.getRequestDispatcher("login.jsp").forward(req,resp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
